package com.xuecheng.manage_course.controller;

import com.xuecheng.api.course.CourseManagerControllerApi;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.course.CourseBase;
import com.xuecheng.framework.domain.course.CourseMarket;
import com.xuecheng.framework.domain.course.Teachplan;
import com.xuecheng.framework.domain.course.TeachplanMedia;
import com.xuecheng.framework.domain.course.ext.CategoryNode;
import com.xuecheng.framework.domain.course.response.*;
import com.xuecheng.framework.exception.CustomExcetionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.utils.XcOauth2Util;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_course.service.CouserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseManagerController extends BaseController implements CourseManagerControllerApi {
    @Autowired
    CouserService couserService;

    @PreAuthorize("hasAuthority('course_find_pic')")
    @GetMapping("/teachplan/list/{id}")
    @Override
    public Teachplan findById(@PathVariable("id") String id) {
        return couserService.findById(id);
    }

    @PostMapping("/teachplan/add")
    @Override
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {
        if (StringUtils.isEmpty(teachplan) || StringUtils.isEmpty(teachplan.getPname()) || StringUtils.isEmpty(teachplan.getCourseid())) {
            throw CustomExcetionCast.getCusException(CommonCode.INVALIDPARAM);
        }
        return couserService.addTeachplan(teachplan);
    }

    //    coursebase/list/"+page+"/"+size
    @PreAuthorize("hasAuthority('course_find_list')")
    @GetMapping("/coursebase/list/{page}/{size}")
    @Override
    public QueryResponseResult findList(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        XcOauth2Util xcOauth2Util=new XcOauth2Util();
        XcOauth2Util.UserJwt header = xcOauth2Util.getUserJwtFromHeader(request);
        String companyId= header.getCompanyId();
        Map map = new HashMap();
        map.put("page", page);
        map.put("size", size);
        map.put("companyId",companyId);
        return couserService.findList(map);

    }

    //    查询课程分类
//    /course/category/list
    @GetMapping("/category/list")
    @Override
    public CategoryNode findCateList() {
        return couserService.findCateList();
    }

    //    根据id查询课程
    @GetMapping("/coursebase/findById/{id}")
    @Override
    public CourseBase findCourseById(@PathVariable("id") String id) {

        return couserService.findCourseById(id);
    }

    /**
     * @Description:添加课程
     * @Param: Course
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @PostMapping("/coursebase/add")
    @Override
    public ResponseResult addCourse(@RequestBody CourseBase courseBase) {
        if (courseBase == null) {
            throw CustomExcetionCast.getCusException(CommonCode.INVALIDPARAM);
        }
        return couserService.addCourse(courseBase);
    }

    /**
     * @Description: 修改课程
     * @Param: id course
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @PostMapping("/updateCourse/{id}")
    @Override
    public ResponseResult updateCourse(@PathVariable("id") String id, @RequestBody CourseBase courseBase) {
        if (courseBase == null) {
            throw CustomExcetionCast.getCusException(CommonCode.INVALIDPARAM);
        }
        return couserService.updateCourse(id, courseBase);
    }

    /**
     * @Description: 课程营销查询
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @GetMapping("/findMarket/{id}")
    @Override
    public CourseMarket findMarketByCourseId(@PathVariable("id") String id) {
        return couserService.findMarketByCourseId(id);
    }

    /**
     * @Description: ] 课程营销添加
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @PostMapping("/updateMarket/{id}")
    @Override
    public ResponseResult saveMarket(@PathVariable("id") String id, @RequestBody CourseMarket courseMarket) {

        return couserService.saveMarket(id, courseMarket);
    }

    /**
     * @Description: 保存图片id和课程id
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @PostMapping("/coursepic/add")
    @Override
    public ResponseResult savePic(@RequestParam("pic") String fileId, @RequestParam("courseId") String courseid) {

        return couserService.savePic(fileId, courseid);
    }

    /**
     * @Description: 查询图片id
     * @Param: courseid
     * @return: fileid
     * @Author: Mr.Yuan
     * @Date:
     */
//    course/coursepic/list/
    @GetMapping("/coursepic/list/{id}")
    @Override
    public FindPicResult findcoursePic(@PathVariable("id") String courseid) {
        return couserService.findcoursePic(courseid);
    }

    /**
     * @Description: 删除课程图片
     * @Param: coureseid
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @DeleteMapping("/coursepic/delete")
    @Override
    public ResponseResult deletePic(@RequestParam("courseId") String courseid) {
        return couserService.deletePic(courseid);
    }

    /**
     * @Description: 课程发布数据模型
     * @Param: courseid
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @GetMapping("/courseView/find/{id}")
    @Override
    public CourseView findAllData(@PathVariable("id") String courseid) {
        return couserService.findAllData(courseid);
    }

    /**
     * @Description: 课程预览
     * @Param: courseid
     * @return: 预览url地址
     * @Author: Mr.Yuan
     * @Date:
     */
    @Override
    @PostMapping("/preview/{id}")
    public PreviewResult coursePreview(@PathVariable("id") String courseid) {

        return couserService.coursePreview(courseid);

    }

    /**
     * @Description: 课程发布接口
     * @Param: courseid
     * @return: 发布结果，响应状态码
     * @Author: Mr.Yuan
     * @Date:
     */
    @PostMapping("/publish/{id}")
    @Override
    public PublishResult coursepublish(@PathVariable("id") String courseid) {
        return couserService.coursepublish(courseid);
    }

    /**
     * @Description: 查询课程信息
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @GetMapping("/courseview/{id}")
    @Override
    public CourseViewResult courseView(@PathVariable("id") String courseid) {
        return couserService.courseView(courseid);
    }

    /**
     * @Description: 添加媒资信息
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @PostMapping("/savemedia")
    @Override
    public ResponseResult savemedia(@RequestBody TeachplanMedia teachplanMedia) {
        return couserService.savemedia(teachplanMedia);
    }
}
